//
//  MessageAndTalkTableViewCell.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/15.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "MessageAndTalkTableViewCell.h"
@interface MessageAndTalkTableViewCell()
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *titleLabel; // 标题
@property (nonatomic, strong) UILabel *detailLabel; // 副标题
@property (nonatomic, strong) UILabel *timeLabel; // 3分钟前
@end

@implementation MessageAndTalkTableViewCell
- (void)setDataDic:(NSDictionary *)dataDic{
    self.titleLabel.text = dataDic[@"title"];
    self.detailLabel.text = dataDic[@"subtitle"];
    self.timeLabel.text = dataDic[@"add_time"];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsZero;
        
        [self.contentView addSubview:self.headerImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.detailLabel];
        [self.contentView addSubview:self.timeLabel];
        
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:lineView];
        self.headerImageView.hidden = YES;
        
        __weak typeof(self)weakself = self;
        [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.mas_top).offset(20 / WIDTH_6S_SCALE);
            make.width.height.mas_offset(40 / WIDTH_6S_SCALE);
            make.left.equalTo(weakself.mas_left);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.headerImageView.mas_top);
            make.left.equalTo(weakself.headerImageView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_offset(20 / HEIGHT_6S_SCALE);
        }];
        [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself.titleLabel.mas_left);
            make.right.equalTo(weakself).offset(-10 / WIDTH_6S_SCALE);
            make.top.equalTo(weakself.titleLabel.mas_bottom).offset(5 / WIDTH_6S_SCALE);
        }];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.titleLabel.mas_top);
            make.right.equalTo(weakself.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(100 / WIDTH_6S_SCALE);
        }];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.headerImageView.mas_top).offset(-10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(weakself);
            make.bottom.equalTo(weakself.detailLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
        }];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakself);
            make.bottom.equalTo(weakself.contentView.mas_bottom).offset(-1);
            make.height.mas_equalTo(0.5);
        }];
        self.timeLabel.text = [self backReleaseTimeWithDateStr:@"2018-06-15 08:23:10"];
    }
    return self;
}

#pragma mark - SET
//- (void)setCheckNumber:(NSNumber *)checkNumber
//{
//    if (checkNumber.integerValue == 0) {
//        self.checkImageView.image = [UIImage imageNamed:@"icon_detail_selected1"];
//    }else{
//        self.checkImageView.image = [UIImage imageNamed:@"icon_detail_selected2"];
//    }
//}
#pragma mark - set
// 返回发布时间dateWithString
- (NSString *)backReleaseTimeWithDateStr:(NSString *)dateWithString{
    // 获取当前NSCalendar
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 设置日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //入参 发布日期
    NSDate *releaseTimer = [formatter dateFromString:dateWithString];
    if ([calendar isDateInToday:releaseTimer]) { // 今天
        //当前时间
        NSDate *nowDate = [NSDate date];
        // 获得日期之间的间隔
        NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        NSDateComponents *components = [calendar components:unit fromDate:releaseTimer toDate:nowDate options:0];
        if (components.hour >= 1) {
            //            formatter.dateFormat = @"今天 HH:mm";
            //            return [formatter stringFromDate:releaseTimer];
            return [NSString stringWithFormat:@"%zd小时前",components.hour];
        }else if(components.minute >= 1){
            return [NSString stringWithFormat:@"%zd分钟前",components.minute];
        }else{
            return @"刚刚";
        }
    }else if([calendar isDateInYesterday:releaseTimer]){  // 昨天
        formatter.dateFormat = @"昨天 HH:mm";
        return [formatter stringFromDate:releaseTimer];
    }else {  // 其他
        formatter.dateFormat = @"MM-dd HH:mm";
        return [formatter stringFromDate:releaseTimer];
    }
    return dateWithString;
}
#pragma mark - init
- (UIImageView *)headerImageView
{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc]init];
        _headerImageView.backgroundColor = [UIColor clearColor];
        _headerImageView.image = [UIImage imageNamed:@"list_pic"];
    }
    return _headerImageView;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = getColor(blackColor);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = DEF_FontSize_16;
        [_titleLabel sizeToFit];
        _titleLabel.text = @"相识有缘304";
    }
    return _titleLabel;
}
- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = getColor(textColor);
        _detailLabel.textAlignment = NSTextAlignmentLeft;
        _detailLabel.numberOfLines = 0;
        _detailLabel.font = DEF_FontSize_14;
        //[_detailLabel sizeToFit];
        _detailLabel.text = @"怀念你怀念你走了云的天空还";
    }
    return _detailLabel;
}
-(UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textColor = getColor(textColor);
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = DEF_FontSize_12;
        [_timeLabel sizeToFit];
        _timeLabel.text = @"2018-06-15 08:23:10";
    }
    return _timeLabel;
}
@end


